Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pify

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pify

Promisify a callback-style function

  • 2.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
76M
increased by10.47%
Maintainers
1
Weekly downloads
 
Created

What is pify?

The pify npm package is a utility module that converts callback-based functions or methods to return Promises. This is particularly useful when working with older Node.js or JavaScript libraries that do not natively support Promises, allowing developers to write cleaner, more modern asynchronous code using async/await or .then() chaining.

What are pify's main functionalities?

Promisifying a single function

This code sample demonstrates how to promisify Node.js's fs.readFile function using pify. The resulting readFileAsync function returns a Promise that resolves with the file's contents or rejects with an error.

const pify = require('pify');
const fs = require('fs');
const readFileAsync = pify(fs.readFile);

readFileAsync('file.txt', 'utf8').then(data => {
  console.log(data);
}).catch(error => {
  console.error(error);
});

Promisifying an entire module

This code sample shows how to promisify all the functions of the fs module. After promisification, methods like fs.readFile return Promises.

const pify = require('pify');
const fs = pify(require('fs'));

fs.readFile('file.txt', 'utf8').then(data => {
  console.log(data);
}).catch(error => {
  console.error(error);
});

Custom promisification options

This code sample illustrates how to use pify with custom options. The 'exclude' option prevents certain functions from being promisified, while 'multiArgs' allows the promise to resolve with an array of values if the original callback returns multiple arguments.

const pify = require('pify');
const someModule = require('some-module');

const promisifiedModule = pify(someModule, {
  exclude: ['nonAsyncFunction'],
  multiArgs: true
});

promisifiedModule.someFunction().then(result => {
  const [firstResult, secondResult] = result;
  console.log(firstResult, secondResult);
});

Other packages similar to pify

Keywords

FAQs

Package last updated on 26 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc